// pages/tuan/starttuan/starttuan.js
var utils = require('../../../utils/util');

Page({
  gotoPage: utils.gotoPage,

  /**
   * 页面的初始数据
   */
  data: {
    productList: '',
    end: '2029-12-30'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var HOST = getApp().globalData.HOST;
    var that = this;
    var openid = wx.getStorageSync('openid');

    // 2. 获取商品信息列表
    wx.request({
      url: HOST + 'product',
      data: {
        ACTION: 'QUERY'
      },
      success(resp) {
        console.log('product:', resp);
        for (var i = 0; i < resp.data.data.length; i++) {
          var p = resp.data.data[i];
          p.covers = p.covers.split(', ');
          p.desc = p.title + ' ' + p.subtitle
        }
        // TODO
        resp.data.data[1] = resp.data.data[0];
        resp.data.data[2] = resp.data.data[0];
        var productList = resp.data.data;
        that.setData({
          productList: productList
        });
        wx.setStorage({
          key: 'productList',
          data: productList,
        });
      },
    });

    that.setData({
      start: new Date()
    });
  },

  // 选择开团产品
  selectProduct: function(event) {
    this.setData({
      index: event.detail.value,
      pid: this.data.productList[event.detail.value].id
    })
    console.log('pid:', this.data.productList[event.detail.value].id);
  },

  bindStartDateChange: function(event) {
    this.setData({
      begindate: event.detail.value
    })
  },

  bindEndDateChange: function(event) {
    this.setData({
      enddate: event.detail.value
    })
  },

  // 确认开团
  startTuan: function(event) {
    var HOST = getApp().globalData.HOST;
    var that = this;
    var openid = wx.getStorageSync('openid');

    var pid = that.data.pid;
    var begindate = that.data.begindate;
    var enddate = that.data.enddate;

    if (pid == '' || begindate == '' || enddate == '') {
      wx.showToast({
        title: '请先填写申请信息',
        icon: 'none'
      })
      return false;
    }


    wx.request({
      url: HOST + 'group/buy',
      method: 'POST',
      data: {
        openid: openid,
        productid: pid,
        begindate: begindate,
        enddate: enddate
      },
      success: function(resp) {
        console.log('start tuan:', resp)
        if (resp.data.code == '000') {
          wx.redirectTo({
            url: '/pages/tuan/starttuanok/starttuanok',
          })
        } else {
          wx.showModal({
            title: '提示',
            content: resp.data.msg,
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          });
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})